package com.trippartner.client;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.trippartner.client.map.view.CarteView;
import com.trippartner.client.map.view.GeoPointDetailsView;
import com.trippartner.client.menu.view.GlobalMenuView;

public class MainView extends Composite implements MainPresenter.Display {

	private final GlobalMenuView globalMenuView;
	private final CarteView carteView;
	private final GeoPointDetailsView geoPointDetailsView;
	
	private static final String MENU_FIXED_HEIGHT = "25px";
	
	private static final int GEOPOINTDETAILS_FIXED_WIDTH_RATIO = 15;
	
	private HorizontalPanel horizontalPanel;
	
	private DockLayoutPanel dockLayoutPanel;
	
	public MainView() {
		super();
		dockLayoutPanel  = new DockLayoutPanel(Unit.PX);
		dockLayoutPanel.setSize(Window.getClientWidth()+"px", Window.getClientHeight()+"px");
		dockLayoutPanel.addNorth(globalMenuView = new GlobalMenuView(),25);
		
		horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(carteView = new CarteView());
		
		dockLayoutPanel.add(horizontalPanel);
		
		horizontalPanel.add(geoPointDetailsView=new GeoPointDetailsView());
		resize();
		initWidget(dockLayoutPanel);
	}

	public GlobalMenuView getGlobalMenuView() {
		return globalMenuView;
	}

	public CarteView getCarteView() {
		return carteView;
	}

	public GeoPointDetailsView getGeoPointDetailsView() {
		return geoPointDetailsView;
	}
	
	public String getMenuFixedHeight(){
		return MENU_FIXED_HEIGHT;
	}
	
	public int getGeoPointDetailsFixedWidthRatio(){
		return GEOPOINTDETAILS_FIXED_WIDTH_RATIO;
	}

	public void resize() {
		dockLayoutPanel.setSize(Window.getClientWidth()+"px", Window.getClientHeight()+"px");
		horizontalPanel.setWidth(Window.getClientWidth()+"px");
		horizontalPanel.setHeight((Window.getClientHeight()-25)+"px");
		setMapSize(Window.getClientWidth(), Window.getClientHeight());
		geoPointDetailsView.setWidth(getGeoPointDetailsWidth()+"px");
	}
	
	public void setMapSize(Integer width, Integer height) {

		if (width == null && height == null) {
			return;
		} else {
			if (width == null) {
				carteView.getMap().setHeight((height-25-carteView.getAdressInputHeight())+"px");
			} else {
				String fixedWidth = width-getGeoPointDetailsWidth()+"px";
				if (height == null) {
					carteView.getMap().setWidth(fixedWidth);
				} else {
					carteView.getMap().setSize(fixedWidth, height-carteView.getAdressInputHeight()-25+"px");
				}
			}
		}
	}

	private int getGeoPointDetailsWidth(){
		return Math.round(Window.getClientWidth() * GEOPOINTDETAILS_FIXED_WIDTH_RATIO/100);
	}
	
}
